<?php
class Tracer{
	private $db;
	private $no;
	private $summary;
	
	
	function Tracer($db){
		$this->db = $db;
	}
	
	private function getIndividu(){
		$data = $this->db->get_results("
            SELECT 
                no_pancang, 
                no_individu, 
                tgl_tanam, 
                replika, 
                cross_asal_pokok, 
                jenis_tanaman, 
                kategori_pokok, 
                ROUND(DATEDIFF(CURDATE(),tgl_tanam)/365,2) as usia 
            FROM 
                caramel_managerial.biodata_tanaman 
            WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if( $data != '' )
			return $data[0];
		else
			return -1;
	}
	
	private function getMaterial($data){
		$peralatan	 = array(
			array($data['peralatan1'], $data['jml_peralatan1']),
			array($data['peralatan2'], $data['jml_peralatan2']),
			array($data['peralatan3'], $data['jml_peralatan3']),
			array($data['peralatan4'], $data['jml_peralatan4']),
			array($data['peralatan5'], $data['jml_peralatan5']));
	
		$pupuk 		 = array(
			array($data['pupuk1'],$data['jml_pupuk1'], $data['satuan_pupuk1']), 
			array($data['pupuk2'],$data['jml_pupuk2'], $data['satuan_pupuk2']), 
			array($data['pupuk3'],$data['jml_pupuk3'], $data['satuan_pupuk3']),
			array($data['pupuk4'],$data['jml_pupuk4'], $data['satuan_pupuk4']),
			array($data['pupuk5'],$data['jml_pupuk5'], $data['satuan_pupuk5']));
			
		$insektisida 		 = array(
			array($data['insektisida1'],$data['jml_insektisida1'], $data['satuan_insektisida1']), 
			array($data['insektisida2'],$data['jml_insektisida2'], $data['satuan_insektisida2']), 
			array($data['insektisida3'],$data['jml_insektisida3'], $data['satuan_insektisida3']),
			array($data['insektisida4'],$data['jml_insektisida4'], $data['satuan_insektisida4']),
			array($data['insektisida5'],$data['jml_insektisida5'], $data['satuan_insektisida5']));
			
		$herbisida 		 = array(
			array($data['herbisida1'],$data['jml_herbisida1'], $data['satuan_herbisida1']), 
			array($data['herbisida2'],$data['jml_herbisida2'], $data['satuan_herbisida2']), 
			array($data['herbisida3'],$data['jml_herbisida3'], $data['satuan_herbisida3']),
			array($data['herbisida4'],$data['jml_herbisida4'], $data['satuan_herbisida4']),
			array($data['herbisida5'],$data['jml_herbisida5'], $data['satuan_herbisida5']));	
		
		$fungisida 		 = array(
			array($data['fungisida1'],$data['jml_fungisida1'], $data['satuan_fungisida1']), 
			array($data['fungisida2'],$data['jml_fungisida2'], $data['satuan_fungisida2']), 
			array($data['fungisida3'],$data['jml_fungisida3'], $data['satuan_fungisida3']),
			array($data['fungisida4'],$data['jml_fungisida4'], $data['satuan_fungisida4']),
			array($data['fungisida5'],$data['jml_fungisida5'], $data['satuan_fungisida5']));	
		
		$bahan_kimia_lain 		 = array(
			array($data['bahan_kimia_lain1'],$data['jml_bahan_kimia_lain1'], $data['satuan_bahan_kimia_lain1']), 
			array($data['bahan_kimia_lain2'],$data['jml_bahan_kimia_lain2'], $data['satuan_bahan_kimia_lain2']), 
			array($data['bahan_kimia_lain3'],$data['jml_bahan_kimia_lain3'], $data['satuan_bahan_kimia_lain3']),
			array($data['bahan_kimia_lain4'],$data['jml_bahan_kimia_lain4'], $data['satuan_bahan_kimia_lain4']),
			array($data['bahan_kimia_lain5'],$data['jml_bahan_kimia_lain5'], $data['satuan_bahan_kimia_lain5']));	
		
		return array(
			'peralatan'		=> $peralatan,
			'pupuk'			=> $pupuk,
			'insektisida'	=> $insektisida,
			'fungisida'		=> $fungisida,
			'herbisida'		=> $herbisida,
			'bahan_kimia_lain'	=> $bahan_kimia_lain);
	}
	
	private function getPenanaman(){
		$data = $this->db->get_results("SELECT * FROM penanaman WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if( $data == '' )
			return -1;
		
		$data = $data[0];
		$material = $this->getMaterial($data);
		return array(
			'tgl_tanam'			=> $data['tgl_tanam'],
			'no_individu'		=> $data['no_individu'],
			'penanggung_jawab'	=> $data['penanggung_jawab'],
			'material'			=> $material
		);
	}
	
	private function getPerawatan(){
		$data = $this->db->get_results("SELECT * FROM perawatan WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if ( $data == '' )
			return -1;
		
		$tgl = array();
		foreach($data as $d){
			$tgl[] = $d['tgl_perawatan'];
		}
		
		return $tgl;
	}
	
	private function getBungaPohon(){
		$data = $this->db->get_results("SELECT * FROM bunga_pohon WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if ( $data == '' )
			return -1;
		
		$tgl = array();
		foreach($data as $d){
			$tgl[] = $d['tgl_pengecekan'];
		}
		
		return $tgl;
	}
	
	private function getBaggingBetina(){
		$data = $this->db->get_results("SELECT * FROM bagging_betina WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if ( $data == '' )
			return -1;
		
		$tgl = array();
		foreach($data as $d){
			$tgl[] = $d['tgl_bagging'];
		}
		
		return $tgl;
	}
	
	private function getBaggingJantan(){
		$data = $this->db->get_results("SELECT * FROM bagging_jantan WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if ( $data == '' )
			return -1;
		
		$tgl = array();
		foreach($data as $d){
			$tgl[] = $d['tgl_bagging'];
		}
		
		return $tgl;
	}

	private function getPolinasi(){
		$data = $this->db->get_results("SELECT * FROM polinasi WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if ( $data == '' )
			return -1;
		
		$tgl = array();
		foreach($data as $d){
			$tgl[] = $d['tgl_persilangan'];
		}
		
		return $tgl;
	}
	
	private function getPanenBetina(){
		$data = $this->db->get_results("SELECT * FROM panen_betina WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if ( $data == '' )
			return -1;
		
		$tgl = array();
		foreach($data as $d){
			$tgl[] = $d['tgl_panen'];
		}
		
		return $tgl;
	}
	
	
	private function getPanenJantan(){
		$data = $this->db->get_results("SELECT * FROM panen_jantan WHERE `no_individu`='$this->no'", ARRAY_A);
		
		if ( $data == '' )
			return -1;
		
		$tgl = array();
		foreach($data as $d){
			$tgl[] = $d['tgl_panen'];
		}
		
		return $tgl;
	}
	
	
	public function trace($no){
		$this->no = $no;
		
		$result = array(
			'identitas'			=> $this->getIndividu(),
			'penanaman'			=> $this->getPenanaman(),
			'perawatan'			=> $this->getPerawatan(),
			'bunga'				=> $this->getBungaPohon(),
			'baggingJantan'		=> $this->getBaggingJantan(),
			'baggingBetina'		=> $this->getBaggingBetina(),
			'polinasi'			=> $this->getPolinasi(),
			'panenJantan'		=> $this->getPanenJantan(),
			'panenBetina'		=> $this->getPanenBetina(),
		);
		
		print '<pre>';
		print_r($result);
	}

}
?>